package com.itfreer.bpm.activiti.workflow.notaskflow;

import java.util.List;

import org.activiti.bpmn.model.FlowNode;
import org.activiti.engine.ManagementService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
 * 提交流程节点任意节点跳转功能
 * @author gj
 *
 */
@Component
public class JumpCmd {
	
	/**
	 * 流程任务服务
	 */
	@Autowired(required = false)
	protected TaskService taskService;
	
	/**
	 * 流程部署服务
	 */
	@Autowired(required = false)
	protected RepositoryService repositoryService;
	
	/**
	 * 流程
	 */
	@Autowired(required = false)
	protected ManagementService managementService;

	/**
	 * 节点任意跳转功能
	 * @param sexeid 流程实例id
	 * @param targetTaskDefined 实现跳转的目标节点
	 */
	public void jump(String sexeid,String targetTaskDefined){
		
		List<Task> listTasks = taskService.createTaskQuery()
				.processInstanceId(sexeid).active().list();
		Task currentTask = null;
		if(listTasks!=null && listTasks.size()>0) {
			currentTask=listTasks.get(0);
		}
        //当前任务
       // Task currentTask = taskService.createTaskQuery().taskId(taskId).singleResult();
        //获取流程定义
        org.activiti.bpmn.model.Process process = repositoryService.getBpmnModel(currentTask.getProcessDefinitionId()).getMainProcess();
        //获取目标节点定义
        FlowNode targetNode = (FlowNode)process.getFlowElement(targetTaskDefined);
        //删除当前运行任务
        String executionEntityId = managementService.executeCommand(new DeleteTaskCmd(currentTask.getId()));
        //流程执行到来源节点
        managementService.executeCommand(new SetFLowNodeAndGoCmd(targetNode, executionEntityId));
    }
}